<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\controller\admin;

use app\wechat\common\AdminWxController;
use app\admin\common\AdminException;

class Tag extends AdminWxController
{
    public function index()
    {
        $data = $this->_get_pagination_data();

        $data['where_list']['mp_number'] = $this->mp_number;

        $data['total_rows'] = model('wechat/tag')->query_count($data['where_list']);
        $data['page_count'] = intval(ceil($data['total_rows'] / $data['page_size']));
        $data['page_num'] = $data['page_num'] > $data['page_count'] ? 1 : $data['page_num'];

        $data['tag_list'] = model('wechat/tag')->query_list($data['where_list'], $data['page_num'], $data['page_size'], $data['sort_field'], $data['sort_order']);

        $this->display('admin/tag/tag_list', $data);
    }

    public function create()
    {
        $tag_info = model('wechat/tag')->initDefault();

        if (request()->isPost()) {
            $tag_info->acceptData($this->post_array());

            try {
                service('wechat/tag')->create_tag($this->mp_number, $tag_info['name']);
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }

            $this->success('新增成功。', $this->url('index'));
        }

        $data['tag_info'] = $tag_info;
        $this->display('admin/tag/tag_form', $data);
    }

    public function modify($id)
    {
        $tag_info = model('wechat/tag')->findOrFail($id);

        if (request()->isPost()) {
            $tag_info->acceptData($this->post_array());

            try {
                service('wechat/tag')->update_tag($this->mp_number, $tag_info['tagid'], $tag_info['name']);
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }

            $this->success('修改成功。', $this->url('index'));
        }

        $data['tag_info'] = $tag_info;
        $this->display('admin/tag/tag_form', $data);
    }

    public function bulk_delete($ids = array())
    {
        $tag_list = model('wechat/tag')->where('id', 'in', $ids)->column('tag_id');

        foreach ($tag_list as $tag_id) {
            try {
                service('wechat/tag')->remove_tag($this->mp_number, $tag_id);
            } catch (AdminException $ae) {
                $this->error($ae->getString());
            }
        }

        $this->success('删除成功。', $this->url('index'));
    }

    public function download()
    {
        set_time_limit(0);

        try {
            service('wechat/tag')->download_tag_list($this->mp_number);
        } catch (AdminException $ae) {
            $this->error($ae->getString());
        }

        $this->success('下载服务器数据成功。', $this->url('index'));
    }
}